﻿/*
 *  Copyright (c) 2012 Brandon Ellenberger
 *
 *  This file is part of OpSatPlan.
 *
 *  OpSatPlan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpSatPlan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpSatPlan.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace OpSatPlan
{
    public class PredicateDefinition
    {
        private string mName;
        private ReadOnlyCollection<Parameter> mParameters;
		private int? mHashCode;
        public PredicateDefinition(string name, IList<Parameter> parms)
        {
            Assertion.Assert<ArgumentNullException>(name != null, "Predicate definition name cannot be null.");
            Assertion.Assert<UsageException>(name != string.Empty, "Predicate definition name cannot be empty string");
            this.mName = name.ToUpper();
            Assertion.Assert<ArgumentNullException>(parms != null, "Parameter list cannot be null.");
            this.mParameters = new ReadOnlyCollection<Parameter>(parms);
			this.mHashCode = null;
        }
        public string Name
        {
            get
            {
                return this.mName;
            }
        }
        public ReadOnlyCollection<Parameter> Parameters
        {
            get
            {
                return this.mParameters;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else if (obj.GetType() == typeof(PredicateDefinition))
                return this == (PredicateDefinition)obj;
            else
                return false;
        }

        public static bool operator ==(PredicateDefinition x, PredicateDefinition y)
        {
            if (System.Object.ReferenceEquals(x, y))
                return true;
            else if ((object)x == null || (object)y == null)
                return false;
            else
            {
                bool equal = true;
                if (x.Parameters.Count != y.Parameters.Count)
                    return false;
                else
                {
                    return x.GetHashCode() == y.GetHashCode();
                }
            }
        }

        public static bool operator !=(PredicateDefinition x, PredicateDefinition y)
        {
            return !(x == y);
        }

        public override int GetHashCode()
        {
			if (this.mHashCode == null)
			{
	            int result = 17;
	            result = 37 * result + this.mName.ToUpper().GetHashCode();
	            foreach (Parameter p in this.Parameters)
	            {
	                result = 37 * result + p.GetHashCode();
	            }
				this.mHashCode = result;
			}
            return this.mHashCode.Value;
        }
    }
}
